package guiobj;

import gui.MainPanel;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import utils.Utils;

import jsonobj.AttrInfo;
import jsonobj.Co2Info;
import jsonobj.ResInfo;

public class JobPanelController {
	private static Vector<String> jobs = new Vector<String>();
	public static DateFormat f= new SimpleDateFormat("H:mm:ss dd/MM/yy");
	
	public static void addJobInfo(MainPanel p,JobPanelInfo i)
	{
		if(jobs.contains(i.name))
		{
			 updateJobInfo(p,i);
			 return;
		}
		Vector t = new Vector();
		jobs.add(i.name);
		t.addElement(i.name);
		t.addElement(i.clu);
		t.addElement(i.status);
		t.addElement(i.subtime);
		t.addElement(i.schedtime);
		t.addElement(i.runtime);
		t.addElement(i.opt);
		t.addElement(i.exec);
		t.addElement("Cancel");
		p.jrows.addElement(t);
		p.jobTable.addNotify();
		p.jobTable.revalidate();
	}
	
	public static void addJobInfo(MainPanel p,SubmitPanelInfo i)
	{
		Vector t = new Vector();
		jobs.add(i.jname);
		t.addElement(i.jname);
		if(i.clu == null)
			t.addElement("not set");
		else
			t.addElement(i.clu);
		t.addElement("Submitted");
		t.addElement(f.format(new Date()));
		t.addElement("n/a");
		t.addElement("n/a");
		t.addElement("n/a");
		t.addElement(i.exec);
		t.addElement("Cancel");
		p.jrows.addElement(t);
		p.jobTable.addNotify();
		p.jobTable.revalidate();
	}
	
	public static void updateJobInfo(MainPanel p, JobPanelInfo i)
	{
		int index = jobs.indexOf(i.name);
		if(index == -1) return;
		p.jobTable.setValueAt(i.status, index, 2);
		p.jobTable.setValueAt(f.format(i.schedtime), index,  4);
		p.jobTable.setValueAt(f.format(i.runtime), index,  5);
		p.jobTable.setValueAt(i.opt, index,  6);
		p.jobTable.revalidate();
	}
	public static void updateJobInfo(AttrInfo i)
	{
		MainPanel p = Utils.p;
		int index = jobs.indexOf(i.jobname);
		if(index == -1)
		{
			Utils.out("INDEX WAS -1");
			return;
		}
		p.jobTable.setValueAt(i.cluster, index, 1);
		p.jobTable.setValueAt("Scheduled", index, 2);
		try {
			p.jobTable.setValueAt(f.format(new Date((long)(f.parse((String)p.jobTable.getValueAt(index, 3)).getTime()+ 1000*i.mean ))), index,  4);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		p.jobTable.setValueAt("mean: "+Utils.df.format(i.mean)+" low: "+Utils.df.format(i.low_bound), index,  6);
		p.jobTable.revalidate();
	}
	public static void updateJobInfo(ResInfo i)
	{
		MainPanel p = Utils.p;
		int index = jobs.indexOf(i.jobname);
		if(index == -1)
		{
			Utils.out("RES INDEX WAS -1");
			return;
		}
		p.jobTable.setValueAt(i.cluster, index, 1);
		p.jobTable.setValueAt("Scheduled", index, 2);
		p.jobTable.setValueAt(f.format(i.time), index,  4);
		if(i.coeff > -899)
			p.jobTable.setValueAt("% CL: "+Utils.df.format(i.probability)+" LR coeff: "+Utils.df.format(i.coeff), index,  6);
		else
			p.jobTable.setValueAt("% CL: "+Utils.df.format(i.probability)+" LR coeff: n/a", index,  6);
		p.jobTable.revalidate();
	}
	public static void updateJobInfo(Co2Info i)
	{
		MainPanel p = Utils.p;
		int index = jobs.indexOf(i.jobname);
		Utils.out("FINDINg index");
		if(index == -1)
		{
			Utils.out("CO2 INDEX WAS -1");
			return;
		}
		Utils.out("index = "+index);
		p.jobTable.setValueAt(i.cluster, index, 1);
		p.jobTable.setValueAt("Scheduled", index, 2);
		p.jobTable.setValueAt(f.format(i.date), index,  4);
		if(i.pearsons > -2)
			p.jobTable.setValueAt("mean: "+Utils.df.format(i.avg)+" std_dev: "+Utils.df.format(i.std_dev)+" pears: "+Utils.df.format(i.pearsons), index,  6);
		else
			p.jobTable.setValueAt("mean: "+Utils.df.format(i.avg)+" std_dev: "+Utils.df.format(i.std_dev)+" pears: n/a", index,  6);
		Utils.out("DONE");
		p.jobTable.revalidate();
		Utils.out("refuckinvalidated");
	}
	
	
	public static void updateJobStatus(MainPanel p, String jname, String s)
	{
		int index = jobs.indexOf(jname);
		if(index == -1) return;
		p.jobTable.setValueAt(s, index, 2);
		if(s.equals("Running"))
				Utils.p.jobTable.setValueAt(f.format(new Date()), index , 5);
	}
	
	public static void removeJob(MainPanel p, String name)
	{
		int index = jobs.indexOf(name);
		if(index != -1)
		{
			p.jrows.remove(index);
			jobs.remove(index);
			p.jobTable.addNotify();
			p.jobTable.revalidate();
		}
	}
}
